// miniprogram/pages/info/zmyygzs/zmyygzs.js
var app = getApp();
var util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hlxbx: false,
    hsfwc: true,
    twocode_en: true,
    btnEn: false,
    modalHidden: true,//是否隐藏对话框

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.hospital_mz_lxbx();
    this.findOpenid();
    this.hospital_mz_lcbx();
    this.sex_radio();
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../../chooseLib/chooseLib',
      })
      return
    }

  },
  sex_radio: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('sex').get({
      success: function (res) {
        // console.log('马上查询数据库，findcheckbox为:', res.data.sex)
        that.setData({
          sex_radio: res.data.sex,
        })
      }
    })
  },
  hospital_mz_lcbx: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('hospital_mz_lcbx').get({
      success: function (res) {
        that.setData({
          hospital_mz_lcbx: res.data.lcbx,
        })
      }
    })
  },
  hospital_mz_lxbx: function (e) {

    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('hospital_mz_lxbx').get({
      success: function (res) {
        // console.log('马上查询数据库，findcheckbox为:', res.data.lxbx)
        that.setData({
          hospital_mz_lxbx: res.data.lxbx,
        })
      }
    })
  },
  // },
  //确定按钮点击事件
  modalBindaconfirm: function () {
    this.setData({
      modalHidden: !this.data.modalHidden,
      textHeid: false
    })

  },
  twocode_en_false: function () {
    // console.log("修改")
    this.setData({
      twocode_en: true,
      btnEn: false,
    })
  },
  getPhoneNumber(e) {
    var that = this;
    wx.cloud.callFunction({
      name: 'getPhone',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      this.setData({
        phone: res.result,
      })
    }).catch(err => {
    });
  },

  selectlist: function (e) {

    var arr = [], arrzz = [], arrsex = [];  //存放选中id的数组
    var json;
    // console.log('hospital_mz_lcbx:this.data.sex_radio.length', e.currentTarget.dataset.index) 
    // console.log('hospital_mz_lcbx:this.data.hospital_mz_lxbx.length', e.currentTarget.dataset.index.lxbx.length) 
    // console.log('hospital_mz_lcbx:this.data.hospital_mz_lcbx.length', e.currentTarget.dataset.index.sex)

    // if (e.currentTarget.dataset.index.sex) {
    //   console.log('if if i f', e.currentTarget.dataset.index.sex)
    // }
    // else
    // {
    //   console.log('else', e.currentTarget.dataset.index.sex)
    // }

    if (e.currentTarget.dataset.index.lxbx) {
      for (let i = 0; i < this.data.hospital_mz_lxbx.length; i++) {
        if (e.currentTarget.dataset.index.lxbx.includes(this.data.hospital_mz_lxbx[i].name)) {
          // 全选获取选中的值
          json = { "checked": true, "name": this.data.hospital_mz_lxbx[i].name, "value": this.data.hospital_mz_lxbx[i].value }
          // console.log('hospital_mz_lcbx:true' + i, json) 
        }
        else {
          json = { "checked": false, "name": this.data.hospital_mz_lxbx[i].name, "value": this.data.hospital_mz_lxbx[i].value }
          // console.log('hospital_mz_lcbx:false' + i, json)        
        }
        arr.push(json);
      }
    }
    else {
      for (let i = 0; i < this.data.hospital_mz_lxbx.length; i++) {

        json = { "checked": false, "name": this.data.hospital_mz_lxbx[i].name, "value": this.data.hospital_mz_lxbx[i].value }
        // console.log('hospital_mz_lcbx:false' + i, json)        

        arr.push(json);
      }
    }

    // console.log(e.currentTarget.dataset.index.sfwc)
    if (e.currentTarget.dataset.index.sex) {
      for (let i = 0; i < this.data.sex_radio.length; i++) {
        if (e.currentTarget.dataset.index.sex.includes(this.data.sex_radio[i].name)) {
          // 全选获取选中的值
          json = { "checked": true, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
          // console.log('hospital_mz_lcbx:true' + i, json) 
        }
        else {
          json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
          // console.log('hospital_mz_lcbx:false' + i, json)        
        }
        arrsex.push(json);
      }
    }
    else {
      for (let i = 0; i < this.data.sex_radio.length; i++) {
        json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
        arrsex.push(json);
      }
    }
    if (e.currentTarget.dataset.index.lxbx4) {
      for (let i = 0; i < this.data.hospital_mz_lcbx.length; i++) {
        if (e.currentTarget.dataset.index.lxbx4.includes(this.data.hospital_mz_lcbx[i].name)) {
          // 全选获取选中的值
          json = { "checked": true, "name": this.data.hospital_mz_lcbx[i].name, "value": this.data.hospital_mz_lcbx[i].value }
          // console.log('hospital_mz_lcbx:true' + i, json) 
        }
        else {
          json = { "checked": false, "name": this.data.hospital_mz_lcbx[i].name, "value": this.data.hospital_mz_lcbx[i].value }
        }
        arrzz.push(json);
      }
    } else {
      for (let i = 0; i < this.data.hospital_mz_lcbx.length; i++) {
        json = { "checked": false, "name": this.data.hospital_mz_lcbx[i].name, "value": this.data.hospital_mz_lcbx[i].value }
        arrzz.push(json);
      }
    }



    this.setData({
      modalHidden: !this.data.modalHidden,
      textHeid: false,
      name: e.currentTarget.dataset.index.name,
      sfz: e.currentTarget.dataset.index.sfz,
      phone: e.currentTarget.dataset.index.phone,
      address: e.currentTarget.dataset.index.address,
      fromwhere: e.currentTarget.dataset.index.fromwhere,
      hospital_mz_lxbx: arr,
      hospital_mz_lcbx: arrzz,
      sex_radio: arrsex,
      memo: e.currentTarget.dataset.index.memo,



    })
    // console.log('selectlist:', e.currentTarget.dataset.index)
    app.globalData.input_id = "1" + e.currentTarget.dataset.index._id
    // console.log('e.currentTarget.dataset.index', e.currentTarget.dataset.index)
    // console.log('res._id', "1" + e.currentTarget.dataset.index._id)
    if (e.currentTarget.dataset.index.name && e.currentTarget.dataset.index.sfz &&
      e.currentTarget.dataset.index.sex && e.currentTarget.dataset.index.address && e.currentTarget.dataset.index.fromwhere && e.currentTarget.dataset.index.phone) {

      // console.log('信息完整')
      this.setData({
        twocode_en: false,
        btnEn: true,
      })
    }
    // setTimeout(function () {

    //   that.setData({
    //     modalHidden: false,
    //     textHeid: false,        
    //   })
    // }, 1000)
    // console.log('selectlist:', e.currentTarget.dataset.index)
    // console.log('selectlist:', e.currentTarget.dataset.index.fromwhere)
  },

  findOpenid: function () {
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        app.globalData.openid = res.result.openid
        db.collection('base').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', res.data)         
            if (res.data.length) {
              // console.log('res.data', res.data)
              this.setData({
                // list_n: res.data,
                modalHidden: true,
              })
            }
            else {
              // console.log('没查询到数据', res.data)
            }
          })

      }
    })


  },
  // sex: function (e) {
  //   var vlxbx = e.detail.value
  //   // console.log("checkbox-group", e.detail.value)
  //   if (vlxbx != "") {
  //     // console.log("checkbox-true", e.detail.value)
  //     this.setData({
  //       hlxbx: false,
  //     })
  //   }
  //   else {
  //     // console.log("checkbox-false", e.detail.value)
  //     this.setData({
  //       hlxbx: true,
  //     })
  //   }
  // },
  sfwc: function (e) {
    var vsfwc = e.detail.value
    if (vsfwc != "") {
      this.setData({
        hsfwc: false,
        twocode_en: true,
        btnEn: false,
      })
    }
    else {
      this.setData({
        hsfwc: true,
        twocode_en: true,
        btnEn: false,
      })
    }
  },
  createTwoCode: function () {
    wx.redirectTo({
      url: '/pages/createTwoCode/createTwoCode'
    })
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    this.setData({ btnEn: true })
    if (
      e.detail.value.name_mz && e.detail.value.sfzh &&
      e.detail.value.sex && e.detail.value.address && e.detail.value.fromwhere) {
      if (e.detail.value.phone) {
        const db = wx.cloud.database()

        db.collection('base').add({
          data: {
            name: e.detail.value.name_mz,
            sex: e.detail.value.sex,
            sfz: e.detail.value.sfzh,
            phone: e.detail.value.phone,
            address: e.detail.value.address,
            lxbx: e.detail.value.lxbx,
            fromwhere: e.detail.value.fromwhere,
            lxbx4: e.detail.value.lxbx4,
            form: "门诊分诊",
            zqaddress: "门诊楼",
            addtime: util.formatTime(new Date()),
            memo: e.detail.value.memo,
            // memo:
          }
          ,
          success: res => {
            // 在返回结果中会包含新创建的记录的 _id                  
            // wx.showToast({
            //   title: '新增记录成功',
            // })


            app.globalData.input_id = "1" + res._id
            wx.redirectTo({
              url: '/pages/createTwoCode/createTwoCode'
            })
            this.setData({ btnEn: false })
          },
          fail: err => {
            wx.showToast({
              icon: 'none',
              title: '新增记录失败,请找当值人员手工登记！'
            })
            // console.error('[数据库] [新增记录] 失败：', err)
          }
        })

      }
      else {
        // console.log('请更改地址信息：', e.detail.value)
        wx.showToast({
          icon: 'loading',
          title: '请允许获取手机',
        })
        this.setData({ btnEn: false })
      }
    }
    else {
      // console.log('form发生了submit事件，携带数据为：', e.detail.value)
      wx.showToast({
        icon: 'loading',
        title: '请填全星号信息',
      })
      this.setData({ btnEn: false })
    }
    // this.setData({ btnEn: false })
  },

})